package com.snowyee.yudada.scoring;

import com.snowyee.yudada.common.ErrorCode;
import com.snowyee.yudada.exception.BusinessException;
import com.snowyee.yudada.model.entity.App;
import com.snowyee.yudada.model.entity.UserAnswer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ScoringStrategyExecutor {

    // 策略列表
    @Resource
    private List<ScoringStrategy> scoringStrategieList;

    /**
     * 根据策略类型获取策略对象
     */

    public UserAnswer doScore(List<String> choiceList, App app) throws Exception {
        Integer appType = app.getAppType();
        Integer strategy = app.getScoringStrategy();

        if (appType == null || strategy == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"应用配置有误，未找到匹配的策略");
        }

        // 根据注解获取策略
        for (ScoringStrategy scoringStrategy : scoringStrategieList) {
            if (scoringStrategy.getClass().isAnnotationPresent(ScoringStrategyConfig.class)) {
                ScoringStrategyConfig scoringStrategyConfig = scoringStrategy.getClass().getAnnotation(ScoringStrategyConfig.class);
                if (scoringStrategyConfig.appType() == appType && scoringStrategyConfig.scoringStrategy() == strategy) {
                    return scoringStrategy.doScore(choiceList, app);
                }
            }
        }

        throw new BusinessException(ErrorCode.SYSTEM_ERROR,"应用配置有误，未找到匹配的策略");
    }
}
